(function () {
    var body=$("body");
    var win=tag.div("w600;h0;poa;ofh;tr300;t"+body.height()/2);
    window.srhData={
        showing:false
    };
    body.append(win);
    function showWin() {
        //如果被藏着 就显示
        if(!srhData.showing){
            srhData.srh.focus();
            win.mst("h400");
            win.css({
                "left":body.width()/2-win.width()/2,
                "top":body.height()/2-200
            });
            srhData.showing=true;
        }
    }
    function hideWin() {
        //如果状态为显示
        if (srhData.showing){
            srhData.showing=false;
            win.mst("h0;t"+body.height()/2)
        }
    }
    window.hideWin=hideWin;
    window.showWin=showWin;
    //没有窗口 可以触发该事件

    //init win
    (function () {
        //
        var win2=tag.div("w100%;h100%;por");
        win.append(win2);
        //
        var url=body.css("background-image");
        var blurBg=tag.div("w700;h500;poa;t-50;l-50;bgi"+url).css("filter","blur(10px)");
        win2.append(blurBg);
        //
        var win3=tag.div("poa;t0;w100%;h100%;ofh;bgrgba(0,0,0,0.3)");
        win2.append(win3);
        //顶栏
        var status=tag.div("w600;h30;bgrgba(0,0,0,0.4)");
        win3.append(status);
        (function () {
            //名字
            var img=$("<img src='data/app/setting/images/icon.png'>")
                .mst("fl;w20;h20;m5");
            var name=tag.span().html("搜 索").mst("fl;mt4;ml6;fz16;c#fff");
            status.append(name);

            //关闭按钮
            var close=tag.canvas().attr({
                "width":"30",
                "height":"30"
            }).mst("bgrgba(0,0,0,0);fr;cup;tr300")
                .hover
                (
                    function () {
                        $(this).mst("bg#f00");
                        drawClose(true);
                    },
                    function () {
                        $(this).mst("bgrgba(0,0,0,0)");
                        drawClose(false);
                    });
            var o=close[0].getContext("2d");
            function drawClose(isHover) {
                o.clearRect(0,0,30,30);
                o.beginPath();
                o.moveTo(8,8);
                o.lineTo(22,22);
                o.moveTo(8,22);
                o.lineTo(22,8);
                isHover?o.strokeStyle="#fff":o.strokeStyle="#333";
                o.stroke();
            }
            drawClose(false);
            status.append(close);
            window.searchClose=close;
        })();
        //输入框
        var srh=tag.input("text","").mst("w450;h40;fl;tac;bgnone;fz16;c#fff").attr("placeholder","喵喵喵~").css({
            "border":"none",
            "outline":"none",
            "letter-spacing":"1px"
        });
        srh.do=function () {
            window.location.href="https://www.baidu.com/baidu?wd="+srh.val()+"&tn=monline_4_dg&ie=utf-8";
        };
        var st="w75;tac;cup";
        var type=[
            tag.span().mst(st).html("网页"),
            tag.span().mst(st).html("音乐"),
            tag.span().mst(st).html("翻译"),
            tag.span().mst(st).html("图片")
        ];
        for(var i=0;i<type.length;i++){
            type[i].hover
            (
                function () {
                    $(this).mst("bgrgba(0,0,0,0.8)");
                },
                function () {
                    $(this).mst("bgnone");
                }
            ).mst("tr300").click(function () {
                var temp=$(this).html();
                $(this).html(type[0].html());
                type[0].html(temp);
                //清空推荐内容
                dataBxo.mst("h0");
            });
        }
        win3.append
        (
            //输入框盒子
            tag.div("w900;h40;bgrgba(0,0,0,0.6);ofh").append
            (
                //搜索内容类型
                tag.span().mst("fl;w75;h40;bgrgba(0,0,0,0.2);tr300;lh40px;c#fff;cup;ofh").append
                (
                    tag.div("w300;h40").append
                    (
                        type[0],
                        type[1],
                        type[2],
                        type[3]
                    )
                ).hover
                (
                    function () {
                        $(this).width("300");
                    },
                    function () {
                        $(this).width("75");
                    }
                ),
                //
                srh,
                tag.div("dib;w75;tac;lh40;c#fff;bgrgba(0,0,0,0.3);cup").html("GO")
                    .click(function () {
                        switch (type[0].html()){
                            case "网页":
                                srh.do();
                                break;
                            case "音乐":
                                alert("开发中");
                                break;
                            case "翻译":
                                alert("开发中");
                                break;
                            case "图片":
                                alert("开发中");
                                break;
                        }
                    })
            )
        );
        //json数据容器
        var dataBxo=tag.div("w600;h0;bgrgba(0,0,0,0.2);ofh;tr300;por").hover
        (
            function () {
                dataTarget.mst("bgrgba(0,0,0,0.4)");
            },
            function () {
                dataTarget.mst("bgnone")
            }
        );
        var dataTarget=tag.div("poa;t-30;w100%;h30;l0;bgrgba(0,0,0,0.4);tr100");
        dataTarget.topTemp=-1;
        dataTarget.topFix=function () {
            dataTarget.mst("t"+dataTarget.topTemp*30)
        };
        dataBxo.append(dataTarget);
        win3.append(dataBxo);
        var dataTag=[];
        //子标签*10
        for(var j=0;j<10;j++){
            var div=tag.div("w100%;h30;lh30;ofh;c#fff;tac;poa;t"+j*30).html(Math.random())
                .attr("data-tgt",j)
                //鼠标进入 背景跟着来
                .mouseover(function () {
                    //位置参数
                        dataTarget.topTemp=$(this).attr("data-tgt");
                        //位置修复
                        dataTarget.topFix();
                    })
                //鼠标点击
                .click(function () {
                  srh.val($(this).html());
                  srh.do();
                });
            dataTag.push(div);
            dataBxo.append(div);
        }
        //回首望月
        window.webCb=function (json) {
            //移花接木
            json=json.s;
            //偷梁换柱
            for(var i=0;i<json.length;i++){
                dataTag[i].html(json[i])
            }
            //改天换地
            dataBxo.mst("h"+json.length*30);
        };
        window.musicCb=function (json) {

        };
        window.trsCb=function (json) {

        };

        srh.focus().bind("input propertychange change",function () {
            if(!srhData.showing){
                showWin();
            }
            switch(type[0].html()){
                case "网页":
                    loadJs("http://suggestion.baidu.com/su?wd="+srh.val()+"&cb=window.webCb");
                    break;
                case "音乐":
                    break;
                case "翻译":
                    break;
                case "图片":
                    break;
            }
        })
            .keydown(function (e) {
                e=e.which;
                if(e===38||e===40){
                    dataTarget.mst("bgrgba(0,0,0,0.4)");
                    e===38?
                        dataTarget.topTemp>-1&&dataTarget.topTemp--:
                        dataTarget.topTemp<dataBxo.height()/30&&dataTarget.topTemp++;

                    dataTarget.topTemp!==-1&&
                    dataTarget.topTemp<dataBxo.height()/30&&
                    srh.val(dataTag[dataTarget.topTemp].html());
                    dataTarget.mst("t"+(dataTarget.topTemp*30));
                    return false
                }else if ( e===13){
                    srh.do()
                }
            });
        srhData.srh=srh;

    })();

})();